﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace RMS.DataAccess.Common
{
    public static class TypeConverter
    {
        public static Dictionary<string, object> ToDictionary(DataRow row)
        {
            var list = new Dictionary<string, object>();

            foreach (DataColumn columnn in row.Table.Columns)
            {
                list.Add(columnn.Caption, row[columnn.Caption]);
            }

            return list;
        }

        public static string ToString(object value)
        {
            return (value != null) ? value.ToString().Trim() : null;
        }

        public static int ToInt32(object value)
        {
            try
            {
                string val = value.ToString();
                val = val.Replace(",", string.Empty);
                val = val.Replace(".", string.Empty);
                return Convert.ToInt32(val);
            }
            catch (Exception)
            {

                return new int();
            }
        }

        public static decimal ToDecimal(object value)
        {
            return Convert.ToDecimal(value.ToString());
        }

        public static DateTime ToDateTime(object value)
        {
            return Convert.ToDateTime(value.ToString());
        }

        public static bool ToBoolean(object value)
        {
            return Convert.ToBoolean(value.ToString());
        }

        public static byte ToByte(object value)
        {
            return Convert.ToByte(value.ToString());
        }

        public static int? ToNullableInt32(object value)
        {
            if (value != null)
            {
                if (!String.IsNullOrEmpty(value.ToString()))
                {
                    string val = value.ToString();
                    val = val.Replace(",", string.Empty);
                    val = val.Replace(".", string.Empty);

                    return Int32.Parse(val);
                }
            }

            return new int?();
        }

        public static long? ToNullableInt64(object value)
        {
            if (value != null)
            {
                if (!String.IsNullOrEmpty(value.ToString()))
                {
                    string val = value.ToString();
                    val = val.Replace(",", string.Empty);
                    val = val.Replace(".", string.Empty);

                    return Int64.Parse(val);
                }
            }
            return new int?();
        }

        public static bool? ToNullableBoolean(object value)
        {
            if (value != null)
            {
                if (!String.IsNullOrEmpty(value.ToString()))
                {
                    return Boolean.Parse(value.ToString());
                }
            }

            return new bool?();
        }

        public static DateTime? ToNullableDateTime(object value)
        {
            if (value != null)
            {
                if (!String.IsNullOrEmpty(value.ToString()))
                {
                    return DateTime.Parse(value.ToString());
                }
            }

            return new DateTime?();
        }

        public static decimal? ToNullableDecimal(object value)
        {
            if (value != null)
            {
                if (!String.IsNullOrEmpty(value.ToString()))
                {
                    return Decimal.Parse(value.ToString());
                }
            }
            return new decimal?();
        }
    }
}
